//
//  BaseNavigationController.m
//  GuiJuWo
//
//  Created by chenguibang on 16/3/24.
//  Copyright © 2016年 chenguibang. All rights reserved.
//

#import "BaseNavigationController.h"
#import "UIView+GBExtent.h"

@interface BaseNavigationController (){
    
}
@property(nonatomic,assign)CGFloat keyboardHeight;
@property(nonatomic,assign)CGFloat contentHeight;
@end

@implementation BaseNavigationController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.contentHeight = SCREEN_HEIGHT;
        [self setContentHeight:self.contentHeight];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    self.fd_interactivePopDisabled = NO;
    self.fd_prefersNavigationBarHidden = YES;
    [self creatView];
}

/**
 *  创建基础视图
 */
-(void)creatView{
    self.navBar = [[BaseNavigationBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [self.view addSubview:self.navBar];
    
    self.navBar.backgroundColor = [UIColor whiteColor];
    self.contentView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.navBar.height, SCREEN_WIDTH, SCREEN_HEIGHT-self.navBar.height-49)];
    [self setContentSize:self.contentHeight];
    self.contentView.backgroundColor = UIColorWithString(0xf4f4f4);
    [self.view addSubview:self.contentView];
    
    [RACObserve(self, hideAllNavBar) subscribeNext:^(NSNumber* x) {
        [UIView animateWithDuration:0.3 animations:^{
            self.navBar.height = x.floatValue? 0:64;
            
        }];
    }];
    [RACObserve(self, hidePartNavBar) subscribeNext:^(NSNumber* x) {
        
        [UIView animateWithDuration:0.3 animations:^{
            self.navBar.height = x.floatValue? 20:64;
        }];
    }];
    
    
    
    
    
    
    [RACObserve(self.navBar, height) subscribeNext:^(id x) {
        //如果显示键盘
        if (self.keyboardHeight == 0) {
            if (self.tabBarController.tabBar.hidden) {
                self.contentHeight = SCREEN_HEIGHT-self.navBar.height;
            }else{
                self.contentHeight = SCREEN_HEIGHT-self.navBar.height - 49;
            }
            [self setContentSize:self.contentHeight];
        }else{
            self.contentHeight = SCREEN_HEIGHT - self.navBar.height - self.keyboardHeight;
        }
        self.contentView.frame = CGRectMake(0, self.navBar.height, SCREEN_WIDTH, self.contentHeight);
        
    }];
    [RACObserve(self, keyboardHeight) subscribeNext:^(NSNumber* x) {
        if (self.keyboardHeight == 0) {
            if (self.tabBarController.tabBar.hidden) {
                self.contentHeight = SCREEN_HEIGHT - self.navBar.height;
                
            }else{
                self.contentHeight = SCREEN_HEIGHT - self.navBar.height - 49;
            }
            
//           [self setContentHeight:self.contentHeight];
        }else{
            self.contentHeight = SCREEN_HEIGHT - self.navBar.height - self.keyboardHeight;
        }
         self.contentView.frame = CGRectMake(0, self.navBar.height, SCREEN_WIDTH, self.contentHeight);
    }];
    
    [self.navBar updateLayout];
    [self.navBar addBorderWithType:BorderButtom color:UIColorWithString(0xeeeeee) width:1];
}




/**
 *  键盘监听
 */
-(void)addKeyBoardObserver{
    //监听键盘状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
    //监听输入法状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeInputMode:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)setContentSize:(CGFloat)height{
        [self.contentView layoutIfNeeded];
        CGFloat w = self.contentView.size.width;
        self.contentView.contentSize = CGSizeMake(w,height);
}


- (void)keyboardDidShow:(NSNotification *)notification
{
    NSValue* aValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGRect keyboardFrame = [self.view convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]];
    CGFloat keyboardHeight = keyboardFrame.size.height;
    self.keyboardHeight = keyboardHeight;
}



- (void)keyboardDidHide:(NSNotification *)notification
{
    self.keyboardHeight = 0;

}
-(void)changeInputMode:(NSNotification *)notification{
    NSString *inputMethod = [[UITextInputMode currentInputMode] primaryLanguage];
}


-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self addKeyBoardObserver];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
